from google.appengine.ext import db

def validateUserInfo(name, gender, age, weight):
	error = ""
	if (not (gender in ["male", "female"])):
		error = "Error: check the gender(male or female)!"
	elif (not (age in range(120))):
		error = "Error: check the age(age should be < 120)"
	elif (isUserExist(name)):
		error == "Error: User already exist"
	else:
		error = "NONE"
	return error

def validateUserUpdateInfo(gender, age, weight):
	"""docstring for validateUserInfo"""
	error = ""
	if (not (gender in ["male", "female"])):
		error = "Error: check the gender(male or female)!"
	elif (not (age in range(120))):
		error = "Error: check the age(age should be < 120)"
	else:
		error = "NONE"
	return error


def getUserInfo(name):
	query = db.GqlQuery("SELECT * FROM UsersModel WHERE name=:1", name)
	result = query.fetch(1)
	return result

def isUserExist(str):
	query = db.GqlQuery("SELECT * FROM UsersModel WHERE name=:1", str)
	results = query.fetch(1)
	if results:
		return True 
	else:
		return False 

def isHeartRateDataExist(st):
	query = db.GqlQuery("SELECT * FROM HeartRateModel WHERE workoutStart=:1", st)
	result = query.fetch(1)
	if result:
		return True
	else:
		return False

def getAllHeartRateData(user_name, limit):
	user_object = getUserInfo(user_name)
	query = db.GqlQuery("SELECT * FROM HeartRateModel WHERE user=:1", user_object[0].key())
	data = query.fetch(limit)
	ret = []
	for d in data:
		if d.calorie != "" and d.workoutTime != "":
			ret.append(d)
	return ret
